/* Copyright (c) 2016-2020 Enfry Ltd. All Rights Reserved.
 * 
 * This software is the confidential and proprietary information of Enfry. 
 * You shall not disclose such Confidential Information
 * and shall use it only in accordance with the terms of the agreements
 * you entered into with Enfry.
 */
package com.enfry.common.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * GZip 压缩与解压
 * @Title com.enfry.util.GZips
 * @author Penghu
 * @date 2016年11月10日
 */
public class GZips{
	protected Logger logger = LoggerFactory.getLogger(getClass());
	
    /**
     * GZIP压缩字符串，要求传入编码
     * 
     * @param src       原字符串
     * @param encoding  编码格式
     * @return 得到字节数组
     */
    public static byte[] gzipString(String src, String encoding)
    {
        if (src == null)
            return new byte[0];
        
        try
        {
            return gzip(src.getBytes(encoding));
        }
        catch (UnsupportedEncodingException e)
        {
            return new byte[0];
        }
    }
    
    /**
     * GZIP字节压缩
     * 
     * @param src   原字节数组
     * @return 压缩全的字节数组
     */
    public static byte[] gzip(byte[] src)
    {
        try
        {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            GZIPOutputStream gzip = new GZIPOutputStream(bos);
            gzip.write(src);
            gzip.finish();
            gzip.close();
            
            byte[] dest = bos.toByteArray();
            bos.close();
            
            return dest;
        }
        catch(Exception e)
        {
            return new byte[0];
        }
    }
    
    /**
     * GZIP 解压字节数组
     * 
     * @param src 原字节数组
     * @return 得到解压后的字节数组
     */
    public static byte[] unGZip(byte[] src)
    {
        try
        {
            ByteArrayInputStream bis = new ByteArrayInputStream(src);
            GZIPInputStream gzip = new GZIPInputStream(bis);
            byte[] buf = new byte[1024];
            int num = -1;
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            while ((num = gzip.read(buf, 0, buf.length)) != -1) 
            {
                baos.write(buf, 0, num);
            }
            
            byte[] dest = baos.toByteArray();
            baos.flush();
            baos.close();
            gzip.close();
            bis.close();
            
            return dest;
        }
        catch(Exception e)
        {
            return new byte[0];
        }
    }
    
    /**
     * GZIP解压，得到字符串
     * 
     * @param src       原字节数组
     * @param encoding  编码
     * @return  得到解压后的字节数组，再编码成字符串
     */
    public static String unGZipString(byte[] src, String encoding)
    {
        byte[] dest = unGZip(src);
        try
        {
            return new String(dest, encoding);
        }
        catch (UnsupportedEncodingException e)
        {
            return null;
        }
    }
    
}
